<template>
	<view>
		<u-navbar :is-back="false" :height="height" :border-bottom="false" :background="background" title=" ">
			<view class="slot-wrap">
				<view>
					<view class="title n-flex-row n-justify-between n-align-center">
						<view>
							<view class="tit">{{$t('商城')}}</view>
							<view class="text n-flex-row">
								<view>{{$t('全新正品')}}</view>
								<view class="line"></view>
								<view>{{$t('精选好物')}}</view>
								<view class="line"></view>
								<view>{{$t('急速发货')}}</view>
							</view>
						</view>
						<!-- <view style="text-align: center;" @click="$.to('/pages/mall/sort')">
							<view class="all">ALL</view>
							<view>分类</view>
						</view> -->
					</view>

					<view class="saixuan" v-if="saixuanShow">
						<view class="mybi">
							{{$t('我的幸运币')}}
							<text>{{  bi? Number(bi).toFixed(2) : '0.00'}}</text>
						</view>
						<view class="screen_assembly">
							<view class="nav">
								<u-tabs v-show="!downIcon" :list="goods_navlist" bg-color="" active-color="#01E0DB"
									inactive-color="#698286" :is-scroll="false" :current="current" :font-size="26"
									:gutter="10" @change="change"></u-tabs>
								<view class="text" v-if="downIcon">{{$t('幸运币区间')}}</view>
							</view>
							<view class="down_view" v-if="!downIcon" @click="downExpand">
								<u-icon name="arrow-down-fill" size="30" />
							</view>
							<view class="down_view" v-if="downIcon" @click="downClose">
								<u-icon name="arrow-up-fill" size="30" />
							</view>
						</view>
					</view>
				</view>
			</view>
		</u-navbar>

		<view class="banner">
			<u-swiper height="300" :border-radius="0" :list="bannerlist" @click="handleBanner"></u-swiper>
		</view>

		<view class="search" v-if="sc_search">
			<view class="box" @click="search">
				<view class="left">
					<u-icon name="search" /> <text class="text">{{ placeholder }}</text>
				</view>
				<view class="btn">
					<u-button :ripple="true" ripple-bg-color="#909399" @click.stop="searchBtn">{{$t('搜索')}}</u-button>
				</view>
			</view>
		</view>
		<view class="menu n-flex-row n-wrap">
			<view class="list" v-for="(item, idx) in navList" :key="idx"
				@click="$.to('/pages/mall/list?id=' + item.id + '&name=' + item.title)">
				<image :src="item.image" mode=""></image>
				<view>{{ item.title }}</view>
			</view>
		</view>
		<view class="saixuan" v-if="!saixuanShow">
				<view class="mybi">
					{{$t('我的幸运币')}}
					<text>{{  bi? Number(bi).toFixed(2) : '0.00'}}</text>
				</view>
				<view class="screen_assembly">
					<view class="nav">
						<u-tabs v-show="!downIcon" :list="goods_navlist" bg-color="" active-color="#01E0DB"
							inactive-color="#698286" :is-scroll="false" :current="current" :font-size="26"
							:gutter="10" @change="change"></u-tabs>
						<view class="text" v-if="downIcon">{{$t('幸运币区间')}}</view>
					</view>
					<view class="down_view" v-if="!downIcon" @click="downExpand">
						<u-icon name="arrow-down-fill" size="30" />
					</view>
					<view class="down_view" v-if="downIcon" @click="downClose">
						<u-icon name="arrow-up-fill" size="30" />
					</view>
				</view>
			</view>
		

		<!-- <view class="centerImge-box">
       			 <image  @tap.stop="gotobanner(centerImge)" :src="centerImge.image" mode="" @></image>
       		 </view> -->
		<view class="goodsList">
			<view class="list" v-for="(item, idx) in goodsList" :key="idx" @click="details(item.id)">
				<view>
					<image :src="item.image" mode=""></image>
				</view>
				<view class="info">
					<view class="tit">
						{{ item.title }}
					</view>
					<view class="price-info">
						<view class="price">
							{{$t('¥')}}
							<text style="font-size: 34rpx">{{
      (item.price * 1).toFixed(0)
    }}</text>
						</view>
						<view class="bi">{{ Number(item.integral).toFixed(2) }}{{$t('幸运币')}}</view>
					</view>
				</view>
			</view>
		</view>
		<u-tabbar v-model="current" :list="tabbar" :mid-button="box_view"></u-tabbar>
		<uni-popup ref="screenPopup" type="top">
			<view class="screenPopup">
				<view class="item" v-for="(navItem, index) in goods_navlist" :key="index"
					@click="navPopup(navItem.id, index)">
					{{ navItem.name }}
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import {
		box_view,
		sc_search
	} from "@/utils/keyControl.js";
	// import { tabbar } from "@/common/tabbar.js";
	export default {
		data() {
			return {
				// tabbar: tabbar,
				background: {
					background: "#ffffff",
				},
				goods_navlist: [],
				current: 0,
				bannerlist: [],
				saixuanShow: false,
				height: "67",
				goodsList: [],
				page: 1,
				navList: [],
				bi: "",
				diPic: "",
				centerImge: null,
				placeholder: "苹果手机",
				box_view: false,
				price_id: "",
				downIcon: false,
				sc_search: false,
			};
		},

		computed: {
			tabbar() {
				return [{
						id: 0,
						iconPath: "https://v3mh6.oss-cn-beijing.aliyuncs.com/51api/tab1.png",
						selectedIconPath: "https://v3mh6.oss-cn-beijing.aliyuncs.com/51api/tab2.png",
						text: this.$t("首页"),
						customIcon: false,
						pagePath: "/pages/tabBar/home",
					},
					{
						id: 1,
						iconPath: "https://v3mh6.oss-cn-beijing.aliyuncs.com/51api/tab3.png",
						selectedIconPath: "https://v3mh6.oss-cn-beijing.aliyuncs.com/51api/tab4.png",
						text: this.$t("商城"),
						customIcon: false,
						pagePath: "/pages/tabBar/mall",
					},
					// {
					// 	id: 5,
					// 	iconPath: "https://v3mh6.oss-cn-beijing.aliyuncs.com/51api/tab5.png",
					// 	selectedIconPath: "https://v3mh6.oss-cn-beijing.aliyuncs.com/51api/tab6.png",
					// 	text: this.$t("盒子"),
					// 	midButton: true,
					// 	customIcon: false,
					// 	pagePath: "/pages/tabBar/old_home",
					// },
					{
						id: 2,
						iconPath: "https://v3mh6.oss-cn-beijing.aliyuncs.com/51api/tab7.png",
						selectedIconPath: "https://v3mh6.oss-cn-beijing.aliyuncs.com/51api/tab8.png",
						text: this.$t("仓库"),
						customIcon: false,
						pagePath: "/pages/tabBar/cangku",
					},
					{
						id: 3,
						iconPath: "https://v3mh6.oss-cn-beijing.aliyuncs.com/51api/tab9.png",
						selectedIconPath: "https://v3mh6.oss-cn-beijing.aliyuncs.com/51api/tab10.png",
						text: this.$t("我的"),
						customIcon: false,
						pagePath: "/pages/tabBar/my",
					},
				]
			}
		},


		onPageScroll(e) {
			if (e.scrollTop >= 300) {
				this.height = "145";
				this.saixuanShow = true;
			} else {
				this.height = "67";
				this.saixuanShow = false;
			}
		},
		// 触底
		onReachBottom() {
			if (this.goodsList.length < this.page * 10) {
				return;
			}
			this.page++;
			this.getGoods();
		},
		onLoad() {
			this.box_view = box_view;
			this.getBanner();
			this.getNav();
			this.getGoods();
		},
		onShow() {
			this.sc_search = sc_search;
			this.getBi();
			this.price_id = "";
			// this.page = 1;
			// this.goodsList = [];
			this.getCenterImge();
			this.getNavList();
		},
		methods: {
			gotobanner(info) {
				if (info.link_url_type == 1) {
					//指定盲盒跳转
					uni.navigateTo({
						url: "/pages/home/kaixiang?id=" +
							info.link_url +
							"&music=" +
							this.muteBgMusic,
					});
				} else if (info.link_url_type == 2) {
					//跳转外部网站
					uni.navigateTo({
						url: "/pages/tabBar/h5",
					});
					setTimeout(() => {
						uni.$emit("go_h5", {
							url: info.link_url,
						});
					}, 600);
				}
			},
			getBanner() {
				this.$http({
						url: "api/common/get_advert",
						data: {
							type: 3
						},
					})
					.then((res) => {
						console.log(res, "轮播");
						if (res.data.code == 1) {
							this.bannerlist = res.data.data;
						}
						this.getZhongButu();
					})
					.catch((err) => {});
			},
			/* 点击轮播图 */
			handleBanner(idx) {
				const info = this.bannerlist[idx];
				console.log(info, "info");
				//指定盲盒跳转
				if (info.link_url_type == 1) {
					uni.navigateTo({
						url: "/pages/home/kaixiang?id=" +
							info.link_url +
							"&music=" +
							this.muteBgMusic,
					});
				}
				//跳转外部网站
				if (info.link_url_type == 2) {
					uni.navigateTo({
						url: "/pages/tabBar/h5",
					});
					setTimeout(() => {
						uni.$emit("go_h5", {
							url: info.link_url,
						});
					}, 600);
				}
				/* 内部网站跳转 */
				if (info.link_url_type == 3) {
					/* 首页 */
					if (info.link_url == 2000) {
						uni.switchTab({
							url: "/pages/tabBar/home",
						});
						return false;
					}
					/* 旧的首页 */
					if (info.link_url == 2001) {
						uni.switchTab({
							url: "/pages/tabBar/old_home",
						});
						return false;
					}
					/* 白嫖页 */
					if (info.link_url == 2002) {
						uni.navigateTo({
							url: "/pagesA/pages/my/baipiao",
						});
						return false;
					}
					/* 一周限定 */
					if (info.link_url == 2003) {
						uni.navigateTo({
							url: "/pagesA/pages/my/dailyLimit",
						});
						return false;
					}
				}
				// 4 跳转新闻页面
				if (info.link_url_type == 4) {
					uni.navigateTo({
						url: `/pagesA/pages/my/newsContent?id=${info.news_id}`,
					});
					return false;
				}
				// 5 跳转活动盲盒
				if (info.link_url_type == 5) {
					if (info.source === 8) {
						/* 今日限定活动需要zid */
						uni.navigateTo({
							url: "/pages/home/kaixiang?id=" +
								info.box_id +
								"&boxSource=" +
								info.source +
								"&zid=" +
								info.z_id,
						});
					} else {
						uni.navigateTo({
							url: "/pages/home/kaixiang?id=" +
								info.box_id +
								"&boxSource=" +
								info.source,
						});
					}
					return false;
				}
			},
			getCenterImge() {
				this.$http({
						url: "api/common/get_advert",
						data: {
							type: 4
						},
					})
					.then((res) => {
						console.log(res, "中间");
						if (res.data.code == 1) {
							this.centerImge = res.data.data[0];
						}
					})
					.catch((err) => {});
			},
			//获取中部图片
			getZhongButu() {
				this.$http({
						url: "api/common/get_advert",
						data: {
							type: 4
						},
					})
					.then((res) => {
						if (res.data.code == 1) {
							let pic;
							res.data.data.forEach((item, index) => {
								pic = item.image;
								return;
							});
							this.diPic = pic;
						}
					})
					.catch((err) => {});
			},
			getNav() {
				this.$http({
						url: "api/common/get_goods_menu",
					})
					.then((res) => {
						console.log(res);
						if (res.data.code == 1) {
							this.navList = res.data.data;
						}
					})
					.catch((err) => {});
			},
			//获取幸运币区间
			getNavList() {
				this.$http({
						url: "api/common/get_price ",
						data: {},
					})
					.then((res) => {
						if (res.data.code == 1) {
							this.goods_navlist = res.data.data.map((item) => {
								if (item.end_price == 0) {
									return {
										name: `${item.start_price}`,
										id: item.id,
									};
								} else {
									return {
										name: `${item.start_price}-${item.end_price}`,
										id: item.id,
									};
								}
							});
							let obj = {
								name: this.$t('综合'),
								id: "",
							};
							this.goods_navlist.unshift(obj);
						}
					})
					.catch((err) => {});
			},
			//查询商品列表
			getGoods() {
				this.$http({
						url: "api/common/get_goods_list",
						data: {
							page: this.page,
							price_id: this.price_id,
						},
					})
					.then((res) => {
						console.log(res);
						if (res.data.code == 1) {
							var data = res.data.data;
							this.goodsList = this.goodsList.concat(data);
						}
					})
					.catch((err) => {});
			},
			// 获取幸运币
			getBi() {
				this.bi = uni.getStorageSync("user").integral;
			},
			change(idx) {
				this.current = idx;
				this.price_id = this.goods_navlist[idx].id;
				this.page = 1;
				this.goodsList = [];
				this.getGoods();
			},
			downExpand() {
				this.downIcon = !this.downIcon;
				this.$refs.screenPopup.open();
			},
			downClose() {
				this.downIcon = !this.downIcon;
				this.$refs.screenPopup.close();
			},
			navPopup(id, index) {
				this.goodsList = [];
				this.$refs.screenPopup.close();
				this.downIcon = !this.downIcon;
				this.price_id = id;
				this.getGoods();
				// 原来的逻辑，跳转到搜索页面
				// let keyword = this.placeholder;
				// uni.navigateTo({
				//   url: `/pages/mall/searchList?keyword=${keyword}&navIndex=${index}`,
				// });
			},
			details(id) {
				uni.navigateTo({
					url: "/pages/mall/goodsInfo?id=" + id,
				});
			},
			//搜索
			search() {
				uni.navigateTo({
					url: "/pages/mall/search?placeholder=" + this.placeholder,
				});
			},
			//搜索
			searchBtn() {
				uni.navigateTo({
					url: "/pages/mall/searchList?keyword=" + this.placeholder,
				});
			},
		},
	};
</script>

<style lang="scss">
	@import url("../../static/css/mall.css");

	.search {
		width: 100%;
		padding: 0 20rpx;
		margin: 20rpx 0;

		.box {
			width: 100%;
			height: 70rpx;
			background-color: #f2f2f2;
			display: flex;
			justify-content: space-between;
			align-items: center;
			padding: 0 10rpx;
			border-radius: 5rpx;
			box-sizing: border-box;

			.left {
				margin-left: 10rpx;

				.text {
					margin-left: 10rpx;
					color: #909399;
				}
			}

			.btn {
				/deep/.u-btn--default {
					width: 110rpx;
					height: 57rpx;
					line-height: 60rpx;
					background-color: #414141;
					color: #fff;
					font-size: 26rpx;
					border-color: #414141;
				}
			}
		}
	}

	.saixuan {
		font-family: "Microsoft Yahei";
		padding-top: 10rpx;
		background-color: #fff;

		.mybi {
			margin-left: 20rpx;
			color: #698286;

			text {
				margin-left: 20rpx;
				font-weight: bold;
				font-size: 34rpx;
				color: #000;
			}
		}

		.screen_assembly {
			display: flex;

			.nav {
				width: calc(100% - 60rpx);
				margin-top: 10rpx;
				height: 85rpx;
				padding-left: 30rpx;

				.text {
					text-align: left;
					line-height: 85rpx;
					margin-left: 25rpx;
					font-weight: bold;
				}

				/deep/.u-tabs-scorll-flex {
					justify-content: flex-start;
					gap: 40rpx;
				}

				/deep/.u-tab-item {
					flex: none !important;
					width: auto !important;
				}
			}

			.down_view {
				width: 90rpx;
				height: 85rpx;
				line-height: 85rpx;
				text-align: center;
				background-color: #fff;
				align-self: center;
			}
		}
	}

	.centerImge-box {
		width: 690rpx;
		height: 300rpx;
		margin: 0rpx auto 20rpx auto;

		image {
			width: 100%;
			height: 100%;
		}
	}

	.screenPopup {
		width: 100%;
		height: 210rpx;
		background-color: #fff;
		display: flex;
		flex-flow: wrap;
		gap: 15rpx;
		// margin: 193px 0 0 0;
		// margin: calc(145px + 100rpx) 0 0 0;
		margin: calc(145px + 50rpx) 0 0 0;
		padding: 20rpx;
		z-index: 99;

		.item {
			width: 160rpx;
			height: 75rpx;
			line-height: 75rpx;
			text-align: center;
			background-color: #f4f3f8;
		}
	}
</style>